package api

import (
	"context"
	"fmt"
	"net/http"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/swarm"
	"github.com/docker/docker/client"
	"github.com/gin-gonic/gin"
)

func GetNodesInfo(ctx *gin.Context) {

	cli, err := client.NewClientWithOpts(client.FromEnv)
	fmt.Println(cli)
	if err != nil {
		panic(err)
	}

	var resp = make([]swarm.Node, 0)
	resp, err = cli.NodeList(context.Background(), types.NodeListOptions{})
	fmt.Println(resp)
	fmt.Println(len(resp), "-----------------------")
	if err != nil {
		return
	}

	// NodeInspectWithRaw return shorter info about node, but no cpu and mem etc. data
	// response, sl, err = cli.NodeInspectWithRaw(context.TODO(), resp[0].ID)
	Info := make([]swarm.Node, 0)
	for index, _ := range resp {
		Info = append(Info, resp[index])
	}

	ctx.JSON(http.StatusOK, gin.H{
		"Resonse": Info,
	})
}
